#encoding=utf8
'''
Created on 2012-9-21

@author: Administrator
'''
from Processor import Processor
from models.User import User
from models.UserModel import *
from pub import Func,UserDict
from pub.Config import Config
class UPMClass(Processor):
    '''
    指令：UR (user register)
    备注：用户密码修改
    输入格式：cmd #cmdID#userID#userOldPsw#userNewPsw
    输出格式：cmd #cmdID#userID#result#msg
    '''
    def __init__(self,):
        Processor.__init__(self,)
        self.__outdata=None
        self.__field={}
    def dataValidtor(self,cmdContent):
        if (len(cmdContent)==3):
            self.__field["UserId"] = cmdContent[0]
            self.__field["userOldPassword"] = cmdContent[1]
            self.__field["userNewPassword"] = cmdContent[2]
            return True
        else:
            return False
    def cgPassword(self,):
        '''
        用户修改密码操作
        '''
        cUser = UserDict.getUserFromUserDictById(self.__field["UserId"])
        returnValue=(Config.I_USER_NOT_FOUND,cUser)
        if(cUser): 
            oldPassword = str(cUser.get_user_pass())
            if(oldPassword!=str(self.__field["userOldPassword"])):
                returnValue = (Config.I_USER_OLD_PASSWORD_WRONG,cUser)
            else:
                cUser.set_user_id(self.__field["UserId"])
                cUser.set_user_pass(self.__field["userOldPassword"])
                cUser.set_new_password(self.__field["userNewPassword"])
                cUser.set_socket(self._sockObj)
                mUsers = UserModel()
                result =  mUsers.cgPassword(cUser.get_user_id(), cUser.get_user_pass(), cUser.get_new_password())
                Func.debugInfo(result)
                if(result ==Config.I_USER_CHANGE_PASSWORD_OK ):
                    cUser.set_user_pass(self.__field["userNewPassword"])
                    UserDict.addToUserDict(cUser)
                returnValue = (result,cUser)
        return returnValue
    def outData(self):
        '''
        输出执行结果集
        '''
        (state,cUser) = self.cgPassword()
        result = Config.I_SYS_ZERO
        UserId = Config.I_SYS_ZERO
        if(state==Config.I_USER_CHANGE_PASSWORD_OK):
            result = Config.I_SYS_ONE
            UserId = cUser.get_user_id()
       
        Command = "%s#%s#%s#%s#%s"%\
        (self._cmd,self._cmdId,UserId,result,state)
        self._sockObj.sendLine(str(Command))
        return Config.I_SYS_ONE;